package cj.web.admin.controllers;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.MessageInfoDAO;
import cj.web.admin.domain.MessageInfo;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("messageinfo")
public class MessageInfoController extends BaseController {

	@Autowired
	private MessageInfoDAO messageInfoDao;
	
	@Get("show")
	public String showMenuInfo(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglyb);
		return "/views/message/messageInfoList.vm";
	}
	
	@Post("getList")
    public String getList(MessageInfo messageInfo){
		List<MessageInfo> arr = messageInfoDao.query(messageInfo);
		
		JSONObject jo = new JSONObject();
		
		int size = messageInfoDao.querysize(messageInfo);
		
		jo.put("data", arr);
		jo.put("draw", messageInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglyb);
		return "/views/message/messageInfoDetail.vm";
	}
	
	@Post("create")
	public synchronized String createMenu(MessageInfo messageInfo,HttpServletRequest request){
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		messageInfo.setCode(ReturnUUID.getOrderNo("LKG"));
		messageInfo.setState(Constants.portal.releaseState.state_0);//未处理
		messageInfo.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		messageInfo.setCreateUser(returnSessionObj(request).getAccount());
		messageInfo.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			messageInfoDao.create(messageInfo);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	//上传图片
	@Post("uploadimage")
	public String uploadimage(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	//上传视频
	@Post("uploadvideo")
	public String uploadvideo(@Param("uploadvideo") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadVideoPath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
		
	@Get("deleteById")
	public String deleteById(@Param("id") int id)
	{
		messageInfoDao.deleteById(id);
		return "r:show";
	}
	
	@Get("edit")
	public String editById(Model model,@Param("id") int id){
		MessageInfo messageInfo = messageInfoDao.queryById(id);
		
		model.add("messageInfo", messageInfo);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglyb);
		return "/views/message/messageInfoEdit.vm";
	}
	
	@Post("update")
	public String updateById(MessageInfo messageInfo,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		messageInfo.setUpdateAt(DateUtils.getNowTime());
		messageInfo.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			messageInfoDao.update(messageInfo);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	//已处理
	@Post("updateCheckById")
	public String updateCheckById(@Param("id") int id,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			messageInfoDao.updateById(id, Constants.portal.releaseState.state_1, returnSessionObj(request).getAccount());
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	//已取消
	@Post("updateUnCheckById")
	public String updateUnCheckById(@Param("id") int id,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			messageInfoDao.updateById(id, Constants.portal.releaseState.state_2, returnSessionObj(request).getAccount());
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo.toString();
	}
}